package com.jetinno.utils;

import android.content.Context;
import android.content.res.Configuration;
import android.os.Build;
import android.util.DisplayMetrics;
import android.view.WindowManager;

/**
 * Created by zhangyuncai.
 * Date: 2022/6/29
 */
public class ScreenUtil {
    /**
     * 检测屏幕方向
     *
     * @return
     */
    public static boolean isLand() {
        //获取设置的配置信息
        Configuration mConfiguration = ContextHolder.getResources().getConfiguration();
        //获取屏幕方向
        int ori = mConfiguration.orientation;
        return ori == Configuration.ORIENTATION_LANDSCAPE;
    }

    /**
     * 获取屏幕宽度
     *
     * @param context
     * @return
     */
    public static int getScreenWidth(Context context) {
        return getScreenWidthAndHight(context)[0];
    }

    /**
     * 获取屏幕高度
     *
     * @param context
     * @return
     */
    public static int getScreenHeigth(Context context) {
        return getScreenWidthAndHight(context)[1];
    }


    /**
     * 获取屏幕的像素
     *
     * @param context
     * @return
     */
    public static int[] getScreenWidthAndHight(Context context) {
        int[] wh = new int[2];

        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= 19) {
            wm.getDefaultDisplay().getRealMetrics(dm);
        } else {
            wm.getDefaultDisplay().getMetrics(dm);
        }

        wh[0] = dm.widthPixels;
        wh[1] = dm.heightPixels;
        return wh;
    }

    public static int getDpi(Context context){
        DisplayMetrics dm = context.getResources().getDisplayMetrics();
        WindowManager wm = (WindowManager) context.getSystemService(Context.WINDOW_SERVICE);
        if (Build.VERSION.SDK_INT >= 19) {
            wm.getDefaultDisplay().getRealMetrics(dm);
        } else {
            wm.getDefaultDisplay().getMetrics(dm);
        }
        return dm.densityDpi;
    }
}
